package com.wangshb.upms.generate.generate;

import com.wangshb.upms.generate.Configuration;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 创建java代码
 */
public class JsGenerate extends AbstractGenerate {


    public JsGenerate(Configuration configuration) {
        super(configuration);
    }

    @Override
    public File getFile() {

        Configuration configuration = getConfiguration();

        //1. 创建java - entity 代码
        String tableName = configuration.getTableEntity().getTableName();
        String systemName = tableName.substring(0, tableName.indexOf("_"));
        String jsParentPath = configuration.getJsSrcPath() + "\\api\\" + systemName + "\\" + replaceUnderLineAndUpperCase(tableName.substring(tableName.indexOf("_") + 1), true);
        File dirFile = new File(jsParentPath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        return new File(dirFile.getAbsolutePath() + "\\index.js");
    }

    @Override
    public Map<String, Object> getTemplateParams() {
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("entity_name", getConfiguration().getTableEntity().getEntityName());
        dataMap.put("request_url", getConfiguration().getTableEntity().getTableName().replaceAll("_", "/"));
        return dataMap;
    }

    @Override
    public String getTemplateName() {
        return "templates/pc-js.ftl";
    }
}
